package com.kingkim.electromechanical.usercenter.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.kingkim.electromechanical.commonutils.model.PageBean;
import com.kingkim.electromechanical.usercenter.model.po.Role;
import com.kingkim.electromechanical.usercenter.service.IRoleService;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController {
    /**
     * 日志
     * */
    private static final Logger log =Logger .getLogger(RoleController.class);

    @Resource
    private IRoleService roleService;

    /**
     * 查询所有角色的方法
     */
    @RequestMapping("query")
    public @ResponseBody String QueryForList(@RequestParam("account_role_name") String account_role_name,@RequestParam(value = "page",defaultValue="1") int page, @RequestParam(value = "limit",defaultValue="12")int limit){
        String JSONString = null;
        try {
            Role r = new Role();
            r.setAccount_role_name(account_role_name);
            Map<String,Object> map = new HashMap();
            PageBean<Role> p = roleService.QueryForList(page,limit,r);
            map.put("data",p.getList());
            map.put("code",0);
            map.put("msg","success");
            map.put("count",p.getTotalCount());
            JSONString = JSON.toJSONString(map);
        }catch (Exception e){
            log.error("查询角色列表异常："+e.getMessage());
        }
        return JSONString;
    }


    @RequestMapping("delete")
    public @ResponseBody  String DeleteForList(@RequestParam("ids") String idJSON){
        String JSONString = null;
        try {
            List<String> ids = JSONArray.parseArray(idJSON, String.class);
            Integer flat = roleService.DeleteForList(ids);
            Map map =new  HashMap<String,Object>();
            map.put("result",flat);
            JSONString = JSON.toJSONString(map);
        }catch (Exception e){
            log.error("删除角色信息异常:"+e.getMessage());
        }
        return JSONString;
    }


    @RequestMapping("save")
    public @ResponseBody String SaveOrModify(@ModelAttribute Role role){
        Map map = new HashMap<String, Object>();
        try {
            int flat = roleService.SaveOrModify(role);
            map.put("result",flat);
        }catch (Exception e){
            log.error("保存角色信息异常:"+e.getMessage());
        }
        return JSON.toJSONString(map);
    }



}
